package com.tensquare.gathering.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.gathering.pojo.Gathering;
import com.tensquare.gathering.service.GatheringService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("gathering")
@CrossOrigin
public class GatheringController {
    @Autowired
    private GatheringService gatheringService;
    @PostMapping
    public Result save(@RequestBody Gathering gathering){
        gatheringService.save(gathering);
        return new Result(true, StatusCode.OK,"ok");
    }
    //  GET/gathering 标签全部列表
    @GetMapping
    public Result findAll(){
        List<Gathering>gatherings=gatheringService.findAll();
        return new Result(true,StatusCode.OK,"ok",gatherings);
    }
    // GET/gathering/{gatheringId}根据ID查询
    @GetMapping("/{gatheringId}")
    public Result findByGatheringId(@PathVariable String gatheringId) {
        Gathering gathering=gatheringService.findById(gatheringId);
        return new Result(true,StatusCode.OK,"ok",gathering);
    }
    // PUT/gathering/{gatheringId}修改标签
    @PutMapping("{gatheringId}")
    public Result findByGatheringId(@PathVariable String gatheringId,@RequestBody Gathering gathering) {
        gathering.setId(gatheringId);
        gatheringService.updateByGatheringId(gathering);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/gathering/{gatheringId}根据ID删除
    @DeleteMapping("{gatheringId}")
    public  Result deleteByGatheringId(@PathVariable String gatheringId){
        gatheringService.deleteByGatheringId(gatheringId);
        return new Result(true,StatusCode.OK,"ok");
    }
    /*
    * POST
    /gathering/search/{page}/{size}
    标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page<Gathering> pageData=gatheringService.findByPage(map,page,size);
        PageResult<Gathering> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * POST
    /gathering/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Gathering> list=gatheringService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
    }
    /*GET/gathering/city/{city}/{page}/{size}
            根据城市显示分页内容*/
    @GetMapping("/city/{city}/{page}/{size}")
    public Result city(@PathVariable String city,
                       @PathVariable Integer page,
                       @PathVariable Integer size){
        Page pageData=gatheringService.findByCity(city,page,size);
        PageResult pageResult=new PageResult(pageData.getTotal(),pageData.getRecords());
        return  new Result(true,StatusCode.OK,"ok",pageResult);

    }

}
